/**
 * @author liuxin
 * @e-mail lxtobe5@gmail.com
 * @update 201110.29
 */

package com.lx.utils
{
	/**
	 * Class that contains static utility methods for manipulating and working with Arrays.
	 */	
	public class ArrayUtil
	{
		/**
		 * @param arr   The array that will be checked for the specified value.
		 * @param value The object which will be searched for within the array.
		 * @return True if the array contains the value, False if it does not.
		 */		
		public static function arrayContainsValue(arr:Array,value:*):Boolean
		{
			return (arr.indexOf(value) != -1);
		}
		
		/**
		 * @param arr   The array that will be checked for the specified value.
		 * @param value The object which will be searched for within the array.
		 * @return int  The index of value in array.
		 */		
		public static function valueOfIndex(arr:Array,value:*):int
		{
			return (arr.indexOf(value));
		}
		
		/**
		 *	Remove all instances of the specified value from the array,
		 * 	@param arr   The array from which the value will be removed
		 *	@param value The object that will be removed from the array.
		 */		
		public static function removeValueFromArray(arr:Array,value:*):void
		{
			var flag:Boolean = arrayContainsValue(arr,value);
			if(!flag)
				throw new Error("Didn't find the value in Array");
			var i:int = arr.indexOf(value);
			arr.splice(i, 1);				
		}
		
		/**
		 * @param start  Index of start position
		 * @param end    Index of end position
		 * @return       Character array contained lowercase alphabet.
		 * @example      (["a","b",......,"z"])
		 */		
		public static function letterLowerArray(start:uint=0,end:uint=0):Array
		{
			var min:uint = 97;
			var max:uint = 122;
			return forlettersArray(min,max,start,end);
		}
		
		/**
		 * @param start  Index of start position
		 * @param end    Index of end position
		 * @return       Character array contained uppercase alphabet.
		 * @example      (["A","B",......,"Z"])
		 */		
		public static function letterUpperArray(start:uint=0,end:uint=0):Array
		{
			var min:uint = 65;
			var max:uint = 90;
			return forlettersArray(min,max,start,end);
		}
		
		/**
		 * @param start  Index of start position
		 * @param end    Index of end position
		 * @param min    Ascii code of "a" or "A"
		 * @param max    Ascii code of "z" or "Z"
		 * @return       Character array contained uppercase or lowercase alphabet.
		 * @example      (["a","b",......,"z"]),(["A","B",......,"Z"])
		 */		
		private static function forlettersArray(min:uint,max:uint,start:uint=0,end:uint=0):Array
		{
			var _start:uint;
			var _end:uint;
			if((start<=25 && start>=0) && (end<=25 && end>=0))
			{
				if(start<end)
				{
					_start = start + min;
					_end = end + min;
				}
				
				if(start>end)
				{
					if(end!=0)
						throw new Error("If the parameter \"end\" isn\'t default value \"0\",\"start\" should be less than \"end\"");
					_start = start + min;
					_end = max;
				}
				
				if(start==end)
				{
					if(start!=0)
						throw new Error("If the parameter \"start\" or \"end\" isn\'t default value \"0\" and \"start\" equal to \"end\",I can\'t create an Array");
					_start = min;
					_end = max;
				}
			}else
			{
				throw new Error("The parameter \"start\" or \"end\" beyonds the \"0 - 25\" range");
			}
			
			var a:Array = [];
			for (var i:uint = _start; i <= _end; i++) 
			{
				a.push(String.fromCharCode(i));
			}
			return a;
		}
		
		/**
		 * @param char   Setup a character
		 * @param start  Index of start position
		 * @param end    Index of end position
		 * @param equal  Equal difference
		 * @return       Character array contained a character add a continuous numbers.
		 * @example      (["k1","k2",......,"k100"])
		 */		
		public static function charNumberArray(char:String,start:Number,end:Number,equal:Number=1):Array
		{
			var a:Array = [];
			 
			if(start<end)
			{
				for (var i:Number = start; i <= end; i += equal) 
				{
					a.push(char+i);
				}
			}else
			{
				throw new Error("The parameter \"start\" must be more than \"end\"");
			}
			return a;
		}
		
		/**
		 * @param start  Index of start position
		 * @param end    Index of end position
		 * @param equal  Equal difference
		 * @return       Number array contained a continuous numbers.
		 * @example      ([1,2,......,100])
		 */		
		public static function NumberArray(start:Number,end:Number,equal:Number=1):Array
		{
			var a:Array = [];
			
			if(start<end)
			{
				for (var i:Number = start; i <= end; i+=equal) 
				{
					a.push(i);
				}
			}else
			{
				throw new Error("The parameter \"start\" must be more than \"end\"");
			}
			return a;
		}
		
		/**
		 * @param start  Index of start position
		 * @param end    Index of end position
		 * @param equal  Equal difference
		 * @return       Character array contained a continuous numbers.
		 * @example      (["1","2",......,"100"])
		 */		
		public static function NumberStringArray(start:Number,end:Number,equal:Number=1):Array
		{
			var a:Array = [];
			
			if(start<end)
			{
				for (var i:Number = start; i <= end; i += equal) 
				{
					a.push(String(i));
				}
			}else
			{
				throw new Error("The parameter \"start\" must be more than \"end\"");;
			}
			return a;
		}
		
		/**
		 * @return       Character array contained words from "Monday" to "Sunday".
		 * @example      (["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"])
		 */		
		public static function weekArray():Array
		{
			return ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
		}
	}
}